﻿using CNFM_CROCODILE_LOGO.AOP.OAuth;
using CNFM_CROCODILE_LOGO.Core.Domain.Current;
using CNFM_CROCODILE_LOGO.Core.Domain.Entity;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Core.Repository;
using CNFM_CROCODILE_LOGO.RestClient.Domain;
using CNFM_CROCODILE_LOGO.RestClient.Utils;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNFM_CROCODILE_LOGO.Services.Implementations
{
    /// <summary>
    /// 用户接口实现
    /// </summary>
    public class UserWXServices : IUserWXServices
    {
        private IEfRepository _rep;
        public UserWXServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public ReturnValueModel Login(string loginName, string password)
        {
            if (string.IsNullOrEmpty(loginName))
                return new ReturnValueModel { Code = 400, Msg = "用户名,不能为空!" };
            if (string.IsNullOrEmpty(password))
                return new ReturnValueModel { Code = 400, Msg = "密码,不能为空!" };
            var roleName = string.Empty;

            var userAll = _rep.Where<CrocodileUser>(p => p.Status == 0 && p.LoginName == loginName && p.Password == UtilsHelp.GetMD5(password)).ToList();
            if (userAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "用户不存在!" };
            var userItem = userAll.FirstOrDefault();
            if (!string.IsNullOrEmpty(userItem.RoleId))
                roleName = _rep.FirstOrDefault<Role>(p => p.Id == userItem.RoleId).RoleName;
            else
                return new ReturnValueModel { Code = 400, Msg = "用户不存在!" };
            if (roleName != "企业用户")
                return new ReturnValueModel { Code = 400, Msg = "用户不存在!" };

            var token = JwtHelp.JwtToekn(userItem, roleName);
            TokenModel tokenModel = new TokenModel()
            {
                AccessToken = token,
                UserId = userItem.Id,
                UserName = userItem.UserName,
                RoleId = userItem.RoleId,
                RoleArea = userItem.UnCode,
                RoleName = roleName
            };

            return new ReturnValueModel
            {
                Code = 200,
                Msg = "success!",
                Result = new
                {
                    Data = tokenModel
                }
            };
        }

        /// <summary>
        /// 企业详情
        /// </summary>
        /// <returns></returns>
        public ReturnValueModel GetEnterprise()
        {
            var enterpriseId = _rep.FirstOrDefault<CrocodileUser>(p => p.Id == UserBasic.userId).EnterpriseId;
            var enterpriseList = _rep.Where<Enterprise>(p => p.Status == 0 && p.Id == enterpriseId)
                                 .Select(p => new EnterpriseModels
                                 {
                                     Id = p.Id,
                                     Remarks = p.Remarks,
                                     Status = p.Status,
                                     UserId = p.UserId,
                                     EnterpriseName = p.EnterpriseName,
                                     RegisteredTime = p.RegisteredTime,
                                     UnCode = p.UnCode,
                                     Address = p.Address,
                                     Person = p.Person,
                                     Telephone = p.Telephone,
                                     Email = p.Email,
                                     Details = p.Details,
                                     LegalPerson = p.LegalPerson,
                                     ZipCode = p.ZipCode,
                                     XYLicense = p.XYLicense,
                                     XYStartDate = p.XYStartDate,
                                     XYEndDate = p.XYEndDate,
                                     JYLicense = p.JYLicense,
                                     JYStartDate = p.JYStartDate,
                                     JYEndDate = p.JYEndDate,
                                     ApprovedSum = p.ApprovedSum,
                                     ApprovedName = p.ApprovedName,
                                     ApprovedDepartment = p.ApprovedDepartment
                                 }).ToList();
            if (enterpriseList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "企业状态不可用!" };
            var model = enterpriseList.FirstOrDefault();

            var imageAll = _rep.Where<MediaImage>(p => p.BllId == model.Id).ToList();
            if (imageAll.Count() > 0)
            {
                if (imageAll.Where(p => p.BllType == "企业驯养许可证").Count() > 0)
                    model.XYUrl = string.Format("{0}{1}", UserBasic.Url(), imageAll.Where(p => p.BllType == "企业驯养许可证").FirstOrDefault().FileUrl);

                if (imageAll.Where(p => p.BllType == "企业经营许可证").Count() > 0)
                    model.JYUrl = string.Format("{0}{1}", UserBasic.Url(), imageAll.Where(p => p.BllType == "企业经营许可证").FirstOrDefault().FileUrl);
            }
            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = model } };
        }

        /// <summary>
        /// 企业修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel UpdateEnterprise(EnterpriseModels model)
        {
            var enterpriseId = _rep.FirstOrDefault<CrocodileUser>(p => p.Id == UserBasic.userId).EnterpriseId;
            var enterpriseList = _rep.Where<Enterprise>(p => p.Status == 0 && p.Id == enterpriseId).ToList();
            if (enterpriseList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该企业,请确认是否为可用状态!" };

            var enterpriseItem = enterpriseList.FirstOrDefault();
            enterpriseItem.EnterpriseName = string.IsNullOrEmpty(model.EnterpriseName) ? enterpriseItem.EnterpriseName : model.EnterpriseName;
            enterpriseItem.RegisteredTime = model.RegisteredTime;
            enterpriseItem.UnCode = string.IsNullOrEmpty(model.UnCode) ? enterpriseItem.UnCode : model.UnCode;
            enterpriseItem.Address = string.IsNullOrEmpty(model.Address) ? enterpriseItem.Address : model.Address;
            enterpriseItem.Person = string.IsNullOrEmpty(model.Person) ? enterpriseItem.Person : model.Person;
            enterpriseItem.Telephone = string.IsNullOrEmpty(model.Telephone) ? enterpriseItem.Telephone : model.Telephone;
            enterpriseItem.Email = string.IsNullOrEmpty(model.Email) ? enterpriseItem.Email : model.Email;
            enterpriseItem.LegalPerson = string.IsNullOrEmpty(model.LegalPerson) ? enterpriseItem.LegalPerson : model.LegalPerson;
            enterpriseItem.ZipCode = string.IsNullOrEmpty(model.ZipCode) ? enterpriseItem.ZipCode : model.ZipCode;
            enterpriseItem.XYLicense = string.IsNullOrEmpty(model.XYLicense) ? enterpriseItem.XYLicense : model.XYLicense;
            if (!string.IsNullOrEmpty(model.XYLicense))
            {
                enterpriseItem.XYStartDate = model.XYStartDate;
                enterpriseItem.XYEndDate = model.XYEndDate;
            }
            enterpriseItem.JYLicense = string.IsNullOrEmpty(model.JYLicense) ? enterpriseItem.JYLicense : model.JYLicense;
            if (!string.IsNullOrEmpty(model.JYLicense))
            {
                enterpriseItem.JYStartDate = model.JYStartDate;
                enterpriseItem.JYEndDate = model.JYEndDate;
            }
            enterpriseItem.ApprovedSum = model.ApprovedSum;
            enterpriseItem.ApprovedName = string.IsNullOrEmpty(model.ApprovedName) ? enterpriseItem.ApprovedName : model.ApprovedName;
            enterpriseItem.ApprovedDepartment = string.IsNullOrEmpty(model.ApprovedDepartment) ? enterpriseItem.ApprovedDepartment : model.ApprovedDepartment;
            enterpriseItem.Details = string.IsNullOrEmpty(model.Details) ? enterpriseItem.Details : model.Details;
            enterpriseItem.Remarks = string.IsNullOrEmpty(model.Remarks) ? enterpriseItem.Remarks : model.Remarks;

            _rep.Update(enterpriseItem);

            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
    }
}
